from django.db import models


# Create your models here.

class City(models.Model):
    name = models.CharField(max_length=50, verbose_name="城市名称")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = "city"
        verbose_name = "城市"
        verbose_name_plural = verbose_name


class MachineRoom(models.Model):
    name = models.CharField(max_length=100, verbose_name="机房名称")
    address = models.TextField(blank=True, null=True, verbose_name="机房地址")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="machine_rooms", verbose_name="所属城市")

    class Meta:
        db_table = "machine_room"
        verbose_name = "机房"
        verbose_name_plural = verbose_name
        ordering = ["city__name", "name"]


class Host(models.Model):
    hostname = models.CharField(max_length=50, verbose_name="主机名称")
    ip_address = models.CharField(max_length=50, unique=True, verbose_name="主机地址")
    host_password = models.CharField(max_length=100, verbose_name="主机密码")
    is_ping = models.BooleanField(default=False, verbose_name="是否可ping")
    last_ping_time = models.DateTimeField(blank=True, null=True, verbose_name="最后ping时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="hosts", verbose_name="所属城市")
    machine = models.ForeignKey(MachineRoom, on_delete=models.CASCADE, related_name="hosts", verbose_name="所属机房")

    class Meta:
        db_table = "host"
        verbose_name = "主机"
        verbose_name_plural = verbose_name
        ordering = ["ip_address"]


class HostRecord(models.Model):
    host_count = models.IntegerField(verbose_name="主机数量")
    record_date = models.DateField(verbose_name="统计日期")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name="所属城市")
    machine_room = models.ForeignKey(MachineRoom, on_delete=models.CASCADE, verbose_name="所属机房")

    class Meta:
        db_table = "host_record"
        verbose_name = "主机统计记录"
        verbose_name_plural = verbose_name
        ordering = ["-record_date", "city__name", "machine_room__name"]
